import {Component, ElementRef, EventEmitter, Input, OnInit, Output} from '@angular/core';
import {ButtonSizeType} from '../../enums';
import {appInjector} from '../../../../utils/app-injector';
import {RequestService} from '../../../../../services/system/request.service';
import {FormUtil} from '../../../../utils/form-util';

@Component({
  selector: 'dhl-submit-button',
  templateUrl: './dhl-submitbutton.component.html'
})
export class DhlSubmitbuttonComponent {

  buttonClass = `${ButtonSizeType.DEFAULT}-button`;
  requestService = appInjector().get(RequestService);
  loading = false;

  @Input() disabled;
  @Input() name = '保存';
  @Input() type = 'primary';
  @Input() icon = 'check';
  @Input() elementRef: ElementRef;
  @Input() submitUrl;
  @Input() sizeType = ButtonSizeType.DEFAULT;
  @Output() afterSubmit = new EventEmitter();

  triggerClick() {
    const formData = FormUtil.getData($(this.elementRef.nativeElement));
    this.loading = true;
    this.requestService.post(this.submitUrl, formData).subscribe(data => {
      if (data) {
        this.afterSubmit.emit({value: 'triggerClick'});
      }
      this.loading = false;
    });
  }

}
